
// 获取所有数据
getWuliudanInfo();
function getWuliudanInfo() {
    Ajax({
        url: "http://localhost/h5/php/wuliudan.php",
        success(res){
            sessionStorage.setItem('wuliudanInfo',res)
            res = JSON.parse(res)
            // 页面设置
            sessionStorage.setItem('pageCurrent',1);
            // 计算总页数
            const pageCount = Math.ceil(res.length/10);
            //存储总页数
            sessionStorage.setItem('pageCount',pageCount);
            //一页十条数据显示
            let result = res.slice(0,10);
            //渲染数据
            // console.log(result);
            render(result);
        }
    })
}

// 渲染数据
function render(result) {

    const tbody = document.querySelector("tbody");
    tbody.innerHTML = "";
    result.forEach(item => {
        let str = `
    <tr>
        <td>${item.w_Id}</td>
        <td>${item.w_tracking}</td>
        <td>${item.w_starthouse}</td>
        <td>${item.w_startdate}</td>
        <td>${item.w_overhouse}</td>
        <td>
        <button type="button" class="btn btn-warning btn-sm" data-o='update' data-id='${item.w_Id}' data-bs-toggle="modal" data-bs-target='#wuliudanUpdate'>修改</button>
        <button type="button" class="btn btn-danger btn-sm" data-o='del' data-id='${item.w_Id}'>删除</button>
        </td>
    </tr>
    `
        tbody.innerHTML += str;
    })

}

// 删除与修改
deleteWuliudan()
function deleteWuliudan() {
    const tbody = document.querySelector("tbody")
    tbody.addEventListener("click", function (e) {
        e = e || window.event;
        console.log(e.target.dataset.o);
        console.log(e.target.dataset.id);
        /* 删除操作 */
        if (e.target.dataset.o == 'del') {
            //防止误删
            const flag = window.confirm("您确定要进行删除吗?")
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/wuliudanDelete.php",
                    data: { id: e.target.dataset.id },
                    success(res) {
                        location.reload();
                    }
                })
            }
        }
        if (e.target.dataset.o == "update") {
            const flag = window.confirm("您确定要进行修改吗?")
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/wuliudaneRvise.php",
                    data: { id: e.target.dataset.id },
                    success(res) {
                        res = JSON.parse(res);
                        let tracking = document.getElementById("updateWuliuTracking");
                        let starthouse = document.getElementById("updateWuliuStarthouse");
                        let startdate = document.getElementById("updateWuliuStartdate");
                        let overhouse = document.getElementById("updateWuliuOverhouse");
                        let id = document.getElementById("updateWuliuId");
                        tracking.value = res.w_tracking;
                        starthouse.value = res.w_starthouse;
                        startdate.value = res.w_startdate;
                        overhouse.value = res.w_overhouse;
                        id.value = res.w_Id;
                    }
                })
            }
        }
    })
}

// 实现修改功能
function updateWuliuInfo() {
    let tracking = document.getElementById("updateWuliuTracking").value;
    let starthouse = document.getElementById("updateWuliuStarthouse").value;
    let startdate = document.getElementById("updateWuliuStartdate").value;
    let overhouse = document.getElementById("updateWuliuOverhouse").value;
    let id = document.getElementById("updateWuliuId").value;
    if (!tracking || !starthouse || !startdate || !overhouse) return window.alert("请输入完整数据!!!")
    Ajax({
        url:"http://localhost/h5/php/yuangoupdate.php",
        type: "POST",
        data: { tracking, starthouse, startdate, overhouse,id},
        success(res) {
            sessionStorage.setItem("allwuliudanInfo",res);
            location.reload();
            window.alert("修改成功!!!")
        }
    })
}


/* 实现添加物流信息 */
function addWuliudanInfo() {
    let wtracking = document.getElementById("addWtracking").value
    let wstarthouse = document.getElementById("addWstarthouse").value
    let wstartdate = document.getElementById("addWstartdate").value
    let woverhouse = document.getElementById("addWoverhouse").value

    // console.log(wtracking,wstarthouse,wstartdate,woverhouse);
    /* 非空判断 */
    if (!wtracking || !wstarthouse || !wstartdate || !woverhouse) return window.alert("请输入完整的数据")

    Ajax({
        url: "http://localhost/h5/php/wuliudanAdd.php",
        type: "POST",
        data: { wtracking, wstarthouse, wstartdate, woverhouse },
        success(res) {
            location.reload();
            console.log(res);
        }
    })
}


// 分页
// 首页
function firstPage(){
    //设置当前页 pageCurrent
    sessionStorage.setItem('pageCurrent',1)
    let result = JSON.parse(sessionStorage.getItem('wuliudanInfo'));
    result = result.slice(0,10)
    render(result);
}
//上一页
function upPage(){
    let pageCurrent = +sessionStorage.getItem('pageCurrent') -1
    let wuliudanInfo = JSON.parse(sessionStorage.getItem('wuliudanInfo'))
    //判断是否是第一页
    if(pageCurrent<1){
        pageCurrent = 1
    }
    sessionStorage.setItem('pageCurrent',pageCurrent);
    //计算上一页的数据
    let result = wuliudanInfo.slice((pageCurrent - 1) * 10,pageCurrent * 10)
    render(result);
}
//下一页
function downPage(){
    let pageCurrent = +sessionStorage.getItem('pageCurrent')+1;
    let wuliudanInfo = JSON.parse(sessionStorage.getItem('wuliudanInfo'))
    //获取最后一页
    let pageCount = +sessionStorage.getItem('pageCount')
    if(pageCurrent>pageCount){
        pageCurrent = pageCount
    }
    sessionStorage.setItem('pageCurrent',pageCurrent);
    let result = wuliudanInfo.slice((pageCurrent - 1) * 10,pageCurrent*10)
    render(result);
}
//尾页
function lastPage(){
    let pageCount = +sessionStorage.getItem('pageCount')
    sessionStorage.setItem('pageCurrent',pageCount);
    let wuliudanInfo = JSON.parse(sessionStorage.getItem('wuliudanInfo'))
    let result = wuliudanInfo.slice((pageCount-1)*10);
    render(result);
}
// 查询物流单号
function getTracking(){
    let tracking=document.getElementById("tracking").value;
    Ajax({
        url:"http://localhost/h5/php/wuliudanTracking.php",
        data:{tracking},
        success(res){
            sessionStorage.setItem("wuliudanInfo",res)
            // 2. 设置当前页为第一页
            sessionStorage.setItem("pageCurrent",1)
            // 3. 计算出尾页
            let result = JSON.parse(res)
            sessionStorage.setItem("pageCount",Math.ceil(result.length / 10))
            // 4. 渲染数据
            result = result.slice(0,10)

            render(result);
        }
    })
}


// 模糊查询
function getHouse(){
    let starthouse=document.getElementById("starthouse").value;
    // console.log(starthouse);
    if(starthouse.trim().length===0){
       return window.alert("请输入关键字!!!")
    }
    Ajax({
        url:"http://localhost/h5/php/wuliudanhouse.php",
        data:{starthouse},
        success(res){
            sessionStorage.setItem("wuliudanInfo",res)
            // 2. 设置当前页为第一页
            sessionStorage.setItem("pageCurrent",1)
            // 3. 计算出尾页
            let result = JSON.parse(res)
            sessionStorage.setItem("pageCount",Math.ceil(result.length / 10))
            // 4. 渲染数据
            result = result.slice(0,10)

            render(result);
        }
    })
}

